﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COFE.Core.Validater
{
    /// <summary>
    /// This interface is implemented by vaious attributes to verify PropertyPair's value.
    /// </summary>
    public interface IPropertyValidater
    {
        void Verify(object value);
    }

    /// <summary>
    /// Extends IPropertyValidater.
    /// </summary>
    public static class IPropertyValidaterExtension
    {
        /// <summary>
        /// Return a list of property validaters for a property.
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static IEnumerable<IPropertyValidater> GetPropertyValidaters(object property)
        {
            foreach (var attribute in property.GetType().GetField(property.ToString()).GetCustomAttributes(typeof(IPropertyValidater), false))
                yield return (IPropertyValidater)attribute;
        }

        /// <summary>
        /// Use property validaters of a property to validate a value.
        /// </summary>
        /// <param name="property"></param>
        /// <param name="value"></param>
        public static void VerifyValue(object property, object value)
        {
            foreach (IPropertyValidater pv in GetPropertyValidaters(property))
                pv.Verify(value);
        }

    }
}
